function Reward_Code(){
	var buffer = new StringBuffer();
	var data = {};
	data.act = "Find_Reward";
    data.userid=userid;
    data.comid=comid;
    data.source = source;
    data.version = version;
    data.sign=GetSign(data.act);
    mui.getJSON(index_domain, data, function (json) {
    	if (json.state) {
			$.each(json.data,function(i,info){
			buffer.append('<div class="crmsheet border-bottom" val="'+info.ID+'" type="'+info.Type+'" flag="'+info.Flag+'" money="'+info.Money+'"><div class="ctitle"><div class="cname">'+info.Name+'</div></div><div class="ctext"><input type="text" readonly="readonly" value="'+info.Context+'" style="text-align:right;"/></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
			});
			$("#contentlist1").html(buffer.toString());
			Init_Reward();
		}
    });
	
};

function Init_Reward(){
	mui("#contentlist1").on("tap",".crmsheet",function(){
		obj=$(this);
		var name=$(this).find(".cname").html();
		var rewardid=$(this).attr("val");
		if(name=="迟到罚款")
		{
			OpenWindow(2,"迟到罚款","保存",function(){
				var error=0;
				$("#contentlist2 input").each(function(i,info){
					if(info.value.Trim()=="")
					{
						plus.nativeUI.toast("值不能为空");
						error++;
						return false;
					}
					if(info.value.Trim()=="0")
					{
						plus.nativeUI.toast("值不能为0");
						error++;
						return false;
					}
				});
				if(error>0){return false;}
				var data = {};
				data.val="";
				$("#contentlist2 .crmsheet").each(function(i,info){
					if($(info).find(".begin").length>0){
						data.val+=$(info).find(".begin").val()+"-"+$(info).find(".end").val()+"-"+$(info).find(".money").val()+",";
					}
					if($(info).find(".last").length>0)
					{
						data.val+=$(info).find(".last").val()+"-,";
					}
				});
				data.act = "Save_ChiDaoZaoTui";
				data.rewardid=rewardid;
			    data.userid=userid;
			    data.comid=comid;
			    data.source = source;
			    data.version = version;
			    data.sign=GetSign(data.act);
			    mui.getJSON(index_domain, data, function (json) {
			    	plus.nativeUI.toast(json.msg);
			    	if (json.state) {
			    		CloseWindow();
			    		var first=$("#contentlist2 .crmsheet:first");
			    		obj.find("input").val("迟到"+first.find(".begin").val()+"-"+first.find(".end").val()+"分钟，罚款"+first.find(".money").val()+"元");
			    	}
			    });
			});
			ChiDao_Code($(this).attr("val"));
		}else if(name=="早退罚款")
		{
			OpenWindow(2,"早退罚款","保存",function(){
				var error=0;
				$("#contentlist2 input").each(function(i,info){
					if(info.value.Trim()=="")
					{
						plus.nativeUI.toast("值不能为空");
						error++;
						return false;
					}
					if(info.value.Trim()=="0")
					{
						plus.nativeUI.toast("值不能为0");
						error++;
						return false;
					}
				});
				if(error>0){return false;}
				var data = {};
				data.val="";
				$("#contentlist2 .crmsheet").each(function(i,info){
					if($(info).find(".begin").length>0){
						data.val+=$(info).find(".begin").val()+"-"+$(info).find(".end").val()+"-"+$(info).find(".money").val()+",";
					}
					if($(info).find(".last").length>0)
					{
						data.val+=$(info).find(".last").val()+"-,";
					}
				});
				data.act = "Save_ChiDaoZaoTui";
				data.rewardid=rewardid;
			    data.userid=userid;
			    data.comid=comid;
			    data.source = source;
			    data.version = version;
			    data.sign=GetSign(data.act);
			    mui.getJSON(index_domain, data, function (json) {
			    	plus.nativeUI.toast(json.msg);
			    	if (json.state) {
			    		CloseWindow();
			    		var first=$("#contentlist2 .crmsheet:first");
			    		obj.find("input").val("早退"+first.find(".begin").val()+"-"+first.find(".end").val()+"分钟，罚款"+first.find(".money").val()+"元");
			    	}
			    });
			});
			ZaoTui_Code($(this).attr("val"));
		}else if(name=="加班补贴")
		{
			OpenWindow(2,"加班补贴","保存",function(){
				var data = {};
				data.act = "Save_JiaBan";
				data.rewardid=rewardid;
			    data.userid=userid;
			    data.comid=comid;
			    data.type=$("#txt_type").val();
			    data.money=$("#txt_money").val();
			    data.source = source;
			    data.version = version;
			    data.sign=GetSign(data.act);
			    
			    mui.getJSON(index_domain, data, function (json) {
			    	plus.nativeUI.toast(json.msg);
			    	if (json.state) {
			    		CloseWindow();
			    		var first=$("#contentlist2 .crmsheet:first");
			    		obj.find("input").val("加班"+data.type + (data.type=="按小时"?"，每小时补贴"+data.money+"元":""));
			    	}
			    });
			});
			JiaBan_Code($(this).attr("val"));
		}else if(name=="业绩提成")
		{
			OpenWindow(2,"业绩提成","保存",function(){
				var error=0;
				$("#contentlist2 input").each(function(i,info){
					if(info.value.Trim()=="")
					{
						plus.nativeUI.toast("值不能为空");
						error++;
						return false;
					}
					if(info.value.Trim()=="0")
					{
						plus.nativeUI.toast("值不能为0");
						error++;
						return false;
					}
				});
				if(error>0){return false;}
				var data = {};
				data.val="";
				$("#contentlist2 .crmsheet").each(function(i,info){
					if($(info).find(".begin").length>0){
						data.val+=$(info).find(".begin").val()+"-"+$(info).find(".end").val()+"-"+$(info).find(".money").val()+",";
					}
					if($(info).find(".last").length>0)
					{
						data.val+=$(info).find(".last").val()+"-"+$(info).find(".money").val()+"-,";
					}
				});
				data.act = "Save_YeJi";
				data.rewardid=rewardid;
			    data.userid=userid;
			    data.comid=comid;
			    data.source = source;
			    data.version = version;
			    data.sign=GetSign(data.act);
			    mui.getJSON(index_domain, data, function (json) {
			    	plus.nativeUI.toast(json.msg);
			    	if (json.state) {
			    		CloseWindow();
			    		var first=$("#contentlist2 .crmsheet:first");
			    		obj.find("input").val("完成"+first.find(".begin").val()+"-"+first.find(".end").val()+"元，提成"+first.find(".money").val()+"%");
			    	}
			    });
			});
			YeJi_Code($(this).attr("val"));
		}
		else
		{
			var title=$(this).find(".cname").html();
			var flag=$(this).attr("flag");
			var type=$(this).attr("type");
			var money=$(this).attr("money");
			OpenWindow(2,title,"保存",function(){
				Save_Reward(rewardid);
			});
			OtherReward_Code(title,flag,type,money);
		}
	});
};

function ChiDao_Code(rewardid){
	var buffer = new StringBuffer();
	var data = {};
	data.act = "Find_RewardOption";
	data.rewardid=rewardid;
    data.userid=userid;
    data.comid=comid;
    data.source = source;
    data.version = version;
    data.sign=GetSign(data.act);
    mui.getJSON(index_domain, data, function (json) {
    	if (json.state) {
		$.each(json.data,function(i,info){
			var option=info.Option.split("-");
			if(option.length==2)
			{
				if(i==0)
				{
					buffer.append('<div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">迟到</div><input class="begin" type="tel" value="'+option[0]+'" style="width:20%; text-align:center;"/><div class="txt">-</div><input class="end" type="tel" value="'+option[1]+'" style="width:20%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">分钟</div></div><div class="ctext" style="width:35%;"><div class="txt">罚款</div><input class="money" type="tel" value="'+info.Value+'" style="width:30%; text-align:center;"/><div class="txt">元</div></div></div>');
				}else
				{
					buffer.append('<div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">迟到</div><input class="begin" type="tel" value="'+option[0]+'" readonly="readonly" style="width:20%; text-align:center; color:#dedddd;"/><div class="txt">-</div><input class="end" type="tel" value="'+option[1]+'" style="width:20%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">分钟</div></div><div class="ctext" style="width:35%;"><div class="txt">罚款</div><input class="money" type="tel" value="'+info.Value+'" style="width:30%; text-align:center;"/><div class="txt">元</div></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveRewardOption(this)">×</a></div></div>');
				}
			}else{
				buffer.append('<div id="optionpanel"></div><div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">迟到</div><input class="last" type="tel" value="'+option[0]+'" readonly="readonly" style="width:28%; text-align:center; color:#dedddd;"/><div class="txt">分钟以上</div></div><div class="ctext" style="width:35%;"><div class="txt">罚半天工资</div></div></div>');
			}
		});
		buffer.append('<div id="addrewardoption" class="crmsheet border-bottom addvote">新增一项</div>');
		$("#contentlist2").html(buffer.toString());
		Init_ChiDao();
		}
    });
};
function ZaoTui_Code(rewardid){
	var buffer = new StringBuffer();
	var data = {};
	data.act = "Find_RewardOption";
	data.rewardid=rewardid;
    data.userid=userid;
    data.comid=comid;
    data.source = source;
    data.version = version;
    data.sign=GetSign(data.act);
    mui.getJSON(index_domain, data, function (json) {
    	if (json.state) {
		$.each(json.data,function(i,info){
			var option=info.Option.split("-");
			if(option.length==2)
			{
				if(i==0)
				{
					buffer.append('<div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">早退</div><input class="begin" type="tel" value="'+option[0]+'" style="width:20%; text-align:center;"/><div class="txt">-</div><input class="end" type="tel" value="'+option[1]+'" style="width:20%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">分钟</div></div><div class="ctext" style="width:35%;"><div class="txt">罚款</div><input class="money" type="tel" value="'+info.Value+'" style="width:30%; text-align:center;"/><div class="txt">元</div></div></div>');
				}else
				{
					buffer.append('<div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">早退</div><input class="begin" type="tel" value="'+option[0]+'" readonly="readonly" style="width:20%; text-align:center; color:#dedddd;"/><div class="txt">-</div><input class="end" type="tel" value="'+option[1]+'" style="width:20%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">分钟</div></div><div class="ctext" style="width:35%;"><div class="txt">罚款</div><input class="money" type="tel" value="'+info.Value+'" style="width:30%; text-align:center;"/><div class="txt">元</div></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveRewardOption(this)">×</a></div></div>');
				}
			}else{
				buffer.append('<div id="optionpanel"></div><div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">早退</div><input class="last" type="tel" value="'+option[0]+'" readonly="readonly" style="width:28%; text-align:center; color:#dedddd;"/><div class="txt">分钟以上</div></div><div class="ctext" style="width:35%;"><div class="txt">罚半天工资</div></div></div>');
			}
		});
		buffer.append('<div id="addrewardoption" class="crmsheet border-bottom addvote">新增一项</div>');
		$("#contentlist2").html(buffer.toString());
		Init_ZaoTui();
		}
    });
};
function YeJi_Code(rewardid){
	var buffer = new StringBuffer();
	var data = {};
	data.act = "Find_RewardOption";
	data.rewardid=rewardid;
    data.userid=userid;
    data.comid=comid;
    data.source = source;
    data.version = version;
    data.sign=GetSign(data.act);
    mui.getJSON(index_domain, data, function (json) {
    	if (json.state) {
		$.each(json.data,function(i,info){
			var option=info.Option.split("-");
			if(option.length==2)
			{
				if(i==0)
				{
					buffer.append('<div class="crmsheet border-bottom"><div class="ctext" style="width:60%;"><div class="txt">完成</div><input class="begin" type="tel" value="'+option[0]+'" style="width:35%; text-align:center;"/><div class="txt">-</div><input class="end" type="tel" value="'+option[1]+'" style="width:35%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">元</div></div><div class="ctext" style="width:30%;"><div class="txt">&nbsp;&nbsp;提成</div><input class="money" type="tel" value="'+info.Value+'" style="width:30%; text-align:center;"/><div class="txt">%</div></div></div>');
				}else
				{
					buffer.append('<div class="crmsheet border-bottom"><div class="ctext" style="width:60%;"><div class="txt">完成</div><input class="begin" type="tel" value="'+option[0]+'" readonly="readonly" style="width:35%; text-align:center; color:#dedddd;"/><div class="txt">-</div><input class="end" type="tel" value="'+option[1]+'" style="width:35%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">元</div></div><div class="ctext" style="width:30%;"><div class="txt">&nbsp;&nbsp;提成</div><input class="money" type="tel" value="'+info.Value+'" style="width:30%; text-align:center;"/><div class="txt">%</div></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveRewardOption(this)">×</a></div></div>');
				}
			}else{
				buffer.append('<div id="optionpanel"></div><div class="crmsheet border-bottom"><div class="ctext" style="width:60%;"><div class="txt">完成</div><input class="last" type="tel" value="'+option[0]+'" readonly="readonly" style="width:50%; text-align:center; color:#dedddd;"/><div class="txt">元以上</div></div><div class="ctext" style="width:30%;"><div class="txt">&nbsp;&nbsp;提成</div><input class="money" type="tel" value="'+info.Value+'" style="width:30%; text-align:center;"/><div class="txt">%</div></div></div>');
			}
		});
		buffer.append('<div id="addrewardoption" class="crmsheet border-bottom addvote">新增一项</div>');
		$("#contentlist2").html(buffer.toString());
		Init_YeJi();
		}
    });
};
function JiaBan_Code(rewardid){
	var buffer = new StringBuffer();
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">补贴方式</div><div class="require">*</div></div><div class="ctext"><select id="txt_type" onchange="Change_RewardType(this)"><option value="按员工工资">按员工工资</option><option value="按小时">按小时</option></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div id="butiejine" class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">每小时补贴</div><div class="require">*</div></div><div class="ctext"><input id="txt_money" type="tel" placeholder="填写补贴金额"/></div><div class="carrow">元</div></div>');
	$("#contentlist2").html(buffer.toString());
	var data = {};
	data.act = "Find_RewardOption";
	data.rewardid=rewardid;
    data.userid=userid;
    data.comid=comid;
    data.source = source;
    data.version = version;
    data.sign=GetSign(data.act);
    mui.getJSON(index_domain, data, function (json) {
    	if (json.state && json.data.length>0) {
			var info=json.data[0];
			$("#txt_type").val(info.Option);
			if(info.Option=="按员工工资")
			{
				$("#txt_money").val("");
			}else
			{
				$("#butiejine").removeClass("hidden");
				$("#txt_money").val(info.Value);
			}
		}
    });
};
function OtherReward_Code(title,flag,type,value){
	var buffer = new StringBuffer();
	buffer.append('<div class="crmsheet border-bottom '+((flag==100)?"":"hidden")+'"><div class="ctitle"><div id="cname" class="cname">名称</div><div class="require">*</div></div><div class="ctext"><input id="txt_name" type="text" placeholder="填写名称" value="'+title+'"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom '+((flag==100)?"":"hidden")+'"><div class="ctitle"><div id="cname" class="cname">奖罚</div><div class="require">*</div></div><div class="ctext"><div class="sex"><input id="boy" name="type" type="radio" style="width:16px;" checked="checked" value="1"/><label for="boy">奖励</label></div><div class="sex"><input id="girls" name="type" type="radio" style="width:16px;" value="2"/><label for="girls">罚款</label></div></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div id="cname" class="cname">金额</div><div class="require">*</div></div><div class="ctext"><input id="txt_money" type="tel" placeholder="填写金额" value="'+value+'" onkeyup="Change_NianZhongJiang(this)"/></div><div class="carrow">元</div></div>');
	if(title=="工龄奖"){
		buffer.append('<div class="loading">以年为单位，每年累加<br>例：3年工龄，工龄奖为：<font class="reds">3 * '+value.toFixed(2)+' = '+(3*parseFloat(value)).toFixed(2)+'</font></div>');
	}
	if(title!="" && flag==100){
	buffer.append('<div id="deletesheet" class="deletesheet" style="margin-top:50px;"><div class="name" style="width:100%; text-align:center;">删除该项</div></div>');
	}
	$("#contentlist2").html(buffer.toString());
	$("input[name='type'][value='"+type+"']").prop("checked", true);
	if(title!="" && flag==100){
		get("deletesheet").addEventListener("tap",function(){
		        	plus.nativeUI.confirm("确定要删除吗？", function(event){
						if(event.index==1)
						{
							var data = {};
						    data.act = "Delete_Reward";
						    data.ids = obj.attr("val");
						    data.userid=userid;
							data.comid=comid;
							data.source=source;
							data.sign=GetSign(data.act);
						    mui.getJSON(index_domain, data, function(json) {
								if(json.state)
								{
									obj.hide(200);
									CloseWindow();
									plus.nativeUI.toast(json.msg);
								}else
								{
									plus.nativeUI.alert(json.msg);
								}
								
						    });	
						}
					},"提示", ["取消","确认"]);
				
		});
	}
};
function Init_ChiDao(){
	get("addrewardoption").addEventListener("tap",function(){
		var val= parseFloat($("#contentlist2 .end:last").val())+1;
		if(isNaN(val)){val=0;}
		$("#optionpanel").append('<div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">迟到</div><input class="begin" type="tel" value="'+val+'" readonly="readonly" style="width:20%; text-align:center; color:#dedddd;"/><div class="txt">-</div><input class="end" type="tel" value="0" style="width:20%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">分钟</div></div><div class="ctext" style="width:35%;"><div class="txt">罚款</div><input class="money" type="tel" value="0" style="width:30%; text-align:center;"/><div class="txt">元</div></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveRewardOption(this)">×</a></div></div>');
	});
};
function Init_ZaoTui(){
	get("addrewardoption").addEventListener("tap",function(){
		var val= parseFloat($("#contentlist2 .end:last").val())+1;
		if(isNaN(val)){val=0;}
		$("#optionpanel").append('<div class="crmsheet border-bottom"><div class="ctext" style="width:55%;"><div class="txt">早退</div><input class="begin" type="tel" value="'+val+'" readonly="readonly" style="width:20%; text-align:center; color:#dedddd;"/><div class="txt">-</div><input class="end" type="tel" value="0" style="width:20%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">分钟</div></div><div class="ctext" style="width:35%;"><div class="txt">罚款</div><input class="money" type="tel" value="0" style="width:30%; text-align:center;"/><div class="txt">元</div></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveRewardOption(this)">×</a></div></div>');
	});
};
function Init_YeJi(){
	get("addrewardoption").addEventListener("tap",function(){
		var val= parseFloat($("#contentlist2 .end:last").val())+1;
		if(isNaN(val)){val=0;}
		$("#optionpanel").append('<div class="crmsheet border-bottom"><div class="ctext" style="width:60%;"><div class="txt">完成</div><input class="begin" type="tel" value="'+val+'" readonly="readonly" style="width:35%; text-align:center; color:#dedddd;"/><div class="txt">-</div><input class="end" type="tel" value="0" style="width:35%; text-align:center;" onkeyup="Next_Money(this)"/><div class="txt">元</div></div><div class="ctext" style="width:30%;"><div class="txt">&nbsp;&nbsp;提成</div><input class="money" type="tel" value="0" style="width:30%; text-align:center;"/><div class="txt">%</div></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveRewardOption(this)">×</a></div></div>');
	});
};
function RemoveRewardOption(o){
	$(o).parent().parent().remove();
	$("#contentlist2 .last").val($("#contentlist2 .end:last").val());
};

function Change_RewardType(o){
	if($(o).val()=="按员工工资"){
		$("#butiejine").addClass("hidden");
	}else
	{
		$("#butiejine").removeClass("hidden");
	}
};
function Change_NianZhongJiang(o)
{
	if($(o).attr("placeholder")=="填写金额")
	{ 
		var val=o.value; 
		if(o.value.Trim()=="")
		{
			val=0;
		}
		$("#contentlist2 .reds").html("3 * "+val.toFixed(2)+" = "+(3*parseFloat(val)).toFixed(2));
	}
};
function Next_Money(o){
	var nextinput=$(o).parent().parent().next().find("input")[0];
	var thisvalue = parseFloat(o.value.Trim()==""?0:o.value)+1;
	if(nextinput && nextinput.className=="begin")
	{
		nextinput.value=thisvalue;
	}else{
		$("#contentlist2 .last").val(o.value);
	}
};

function Save_Reward(id){
				if($("#txt_name").val().Trim()=="")
				{
					plus.nativeUI.toast("名称不能为空");
					return false;
				}
				if($("#txt_money").val().Trim()=="")
				{
					plus.nativeUI.toast("金额不能为空");
					return false;
				}
				
				var data = {};
				data.act = "Save_Reward";
				data.id=id;
				data.name=$("#txt_name").val();
				data.type=$("input[name='type']:checked").val();
				data.money=$("#txt_money").val();
			    data.userid=userid;
			    data.comid=comid;
			    data.source = source;
			    data.version = version;
			    data.sign=GetSign(data.act);
			    mui.getJSON(index_domain, data, function (json) {
			    	plus.nativeUI.toast(json.msg);
			    	if (json.state) {
			    		CloseWindow();
			    		if(id==0){
			    			Reward_Code();
			    		}else
			    		{
				    		obj.find(".cname").html(data.name);
				    		obj.attr("type",data.type);
				    		obj.attr("money",data.money).find("input").val(data.money.toFixed(2));
			    		}
			    	}
			    });
};
